Methods and properties that don’t access instance data should be marked as static
for the following reasons:
- Clarity and Intent: Marking a method/property as static makes it clear that the method does not depend on instance data and can be called
without creating an instance of the class. This improves the readability of the code by clearly conveying the member’s intended use.
- Performance: Instance methods/properties in C# require an instance of the class to be called. This means that even if the it doesn’t use any
instance data, the runtime still needs to pass a reference to the instance during the call. For static methods and properties, this overhead is
avoided, leading to slightly better performance.
- Memory Usage: Since instance methods implicitly carry a reference to the instance (the caller object), they can potentially prevent the garbage
collector from collecting the instance whem it is not otherwise referenced. Static members do not carry this overhead, potentially reducing memory
usage.
- Testing: Static members can be easier to test since they do not require an instance of the class. This can simplify unit testing and reduce the
amount of boilerplate code needed to set up tests.
Exceptions
Methods with the following names are excluded because they can’t be made static
: